﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;

namespace IRIS.Controllers
{
    public class ContentController : Controller
    {
        //
        // GET: /Content/
        DataRepo m_Db = new DataRepo();
        
        [HttpGet]
        public ActionResult Index()
        {
            ContentViewModel VM = new ContentViewModel
                                      {m_Articles = m_Db.getArticles(), m_Categories = m_Db.getContentCategories()};
     
            return View(VM);
        }
        
        [HttpGet]
        public ActionResult Edit()
        {
            //vantar ad klara utfærslu
            ContentViewModel VM = new ContentViewModel 
                                      { m_Articles = m_Db.getArticles(), m_Categories = m_Db.getContentCategories() };
            return View(VM);
        }
        
        [HttpGet]
        public ActionResult Create()
        {
            ContentViewModel VM = new ContentViewModel { m_Articles = m_Db.getArticles(), m_Categories = m_Db.getContentCategories() };
            return View(VM);
        }

        [HttpPost]
        [ValidateInput(false)] 
        public ActionResult  Create(ContentViewModel FormData)
        {
            //TODO: Validation
            var fData = new Content();
            fData.ContentText = FormData.m_Content.ContentText;
            fData.ContentTitle = FormData.m_Content.ContentTitle;
            fData.Created = DateTime.Now;
            fData.Author = User.Identity.Name;
            fData.Alive = true;
            fData.Approved = true;
            fData.News = false;
            fData.StaticPage = false;
            UpdateModel(fData);
            m_Db.Add(fData);
            return RedirectToAction("Index");
        }
    }
}
